package short_semester.questionnaire_planet.MyUtil;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.response.PDFResponse.PdfFontUtils;
import short_semester.questionnaire_planet.service.editService.QuestionService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Component
public class PDFUtil {
    @Autowired
    private QuestionnaireDao questionnaireDao;

    @Autowired
    private QuestionSequenceDao questionSequenceDao;

    @Autowired
    private QuestionService questionService;

    public Document createPdf(String outpath) throws DocumentException, IOException {
        //页面大小
        Rectangle rect = new Rectangle(PageSize.A4);//文档竖方向
        //如果没有则创建
        File saveDir = new File(outpath);
        File dir = saveDir.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        //文档属性
        doc.addTitle("下载问卷");
        doc.addAuthor("问卷星球");
        doc.addSubject("请耐心等待");
        //页边空白
        doc.setMargins(40, 40, 40, 40);
        //打开文档
        doc.open();
        return doc;
    }

    public void createFile(Document doc,Questionnaire questionnaire) throws DocumentException{

        //Optional<Questionnaire> optional = questionnaireDao.findById(questionnaireId);

        //Questionnaire questionnaire = questionnaireDao.findById(questionnaireId).get();
        //List<QuestionSequence> questionSequence = questionSequenceDao.findByQuestionnaire(questionnaire);
        Collection<Object> questions = questionService.getQuestions(questionnaire);
        doc.add(PdfFontUtils.getFont(1, questionnaire.getTitle()+"_问卷"));
        doc.add(PdfFontUtils.getFont(5,  " "));
        doc.add(PdfFontUtils.getFont(2,  "问卷情况:"));
        doc.add(PdfFontUtils.getFont(5, "问卷名称:"+questionnaire.getTitle()+"_问卷"));
        if(questionnaire.getType()==null||questionnaire.getType().trim().equals("")){
            doc.add(PdfFontUtils.getFont(5, "问卷类型:普通问卷"));
        }
        else if(questionnaire.getType().equals("vote")){
            doc.add(PdfFontUtils.getFont(5, "问卷类型:投票问卷"));
        }
        else if(questionnaire.getType().equals("sign")){
            doc.add(PdfFontUtils.getFont(5, "问卷类型:报名问卷"));
        }
        else if(questionnaire.getType().equals("exam")){
            doc.add(PdfFontUtils.getFont(5, "问卷类型:考试问卷"));
        }
        else if(questionnaire.getType().equals("card")){
            doc.add(PdfFontUtils.getFont(5, "问卷类型:疫情打卡问卷"));
        }
        doc.add(PdfFontUtils.getFont(5, "问卷描述:"+questionnaire.getDescription()));
        doc.add(PdfFontUtils.getFont(5, "问卷创建时间:"+questionnaire.getCreatedDate()));
        doc.add(PdfFontUtils.getFont(5,  " "));

        for (Object question : questions) {
            if(question instanceof SingleChoiceQuestion){
                doc.add(PdfFontUtils.getFont(2, "第"+(((SingleChoiceQuestion) question).getSequence()+1)+"题:"+((SingleChoiceQuestion) question).getTitle()));
                doc.add(PdfFontUtils.getFont(4, "类型:单选题"));
                List<SingleChoiceOption> options = ((SingleChoiceQuestion) question).getOptions();
                for (SingleChoiceOption option : options) {
                    doc.add(PdfFontUtils.getFont(5, (1+option.getSequence())+"、 "+option.getText()));
                }
                doc.add(PdfFontUtils.getFont(5,  " "));
            }
            else if(question instanceof MultiChoiceQuestion){
                doc.add(PdfFontUtils.getFont(2, "第"+(((MultiChoiceQuestion) question).getSequence()+1)+"题:"+((MultiChoiceQuestion) question).getTitle()));
                doc.add(PdfFontUtils.getFont(4, "类型:多选题"));
                List<MultiChoiceOption> options = ((MultiChoiceQuestion) question).getOptions();
                for (MultiChoiceOption option : options) {
                    doc.add(PdfFontUtils.getFont(5, (1+option.getSequence())+"、 "+option.getText()));
                }
                doc.add(PdfFontUtils.getFont(5,  " "));
            }
            else if(question instanceof FillBlankQuestion){
                doc.add(PdfFontUtils.getFont(2, "第"+(((FillBlankQuestion) question).getSequence()+1)+"题:"+((FillBlankQuestion) question).getTitle()));
                if(questionnaire.getType().equals("card")&&((FillBlankQuestion) question).getQtype()!=null &&((FillBlankQuestion) question).getQtype().equals("card") ){
                    doc.add(PdfFontUtils.getFont(4, "类型:定位题"));
                }
                else
                    doc.add(PdfFontUtils.getFont(4, "类型:填空题"));
                doc.add(PdfFontUtils.getFont(5,  " "));
            }
            else{
                doc.add(PdfFontUtils.getFont(2, "第"+(((ScoreQuestion) question).getSequence()+1)+"题:"+((ScoreQuestion) question).getTitle()));

                doc.add(PdfFontUtils.getFont(4, "类型:评分题"));
                List<ScoreOption> options = ((ScoreQuestion) question).getOptions();
                String context="";
                for (int i=1;i<=options.size();i++) {
                    context=context+i+"分  ";
                }

                doc.add(PdfFontUtils.getFont(5, context));
                doc.add(PdfFontUtils.getFont(5,  " "));

            }
        }

    }
}
